【leetcode】724. Find Pivot Index

Given an array of integers nums, write a method that returns the “pivot” index of this array.
We define the pivot index as the index where the sum of the numbers to the left of the index is equal to the sum of the numbers to the right of the index.
If no such index exists, we should return -1. If there are multiple pivot indexes, you should return the left-most pivot index.

1 基本思路

可以利用空间换时间,依次将从左边累加和从右边累加的结果分别保存在两个数组中,然后逐一比较这两个累加数组,当相等,则返回当前下标。
为什么两个累加数组中,相同索引i所对应的数组元素值想等,该下i标就是所要求的呢?因为左边累加的数组减去当前元素,就是i左边所有元素之和,而右累加数组减去当前元素,就是i右边元素之和,而减去的元素值是同一个,所有i满足条件。

2 完整代码

class Solution {
public:
    int pivotIndex(vector<int>& nums) {
        //获取数组中元素个数
        int len=nums.size();
        int* a=new int[len];
        int* b=new int[len];
        if(len==0)
            return -1;
        if(len==1)
            return 0;
        int sumi=0;
        int sumj=0;
        int i=0;
        int j=len-1;
        for(i=0;i<len;i++)
        {
            sumi=nums[i]+sumi;
            a[i]=sumi;
        }
        for(j=len-1;j>-1;j--)
        {
            sumj=sumj+nums[j];
            b[j]=sumj;
        }
        for(i=0;i<len;i++)
        {
            if(a[i]==b[i])
                return i;
        }
        return -1;
    }
};

文章作者: IrvingBei
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 IrvingBei !
评论
 上一篇
【NLTK基础教程】01-1 三种获取网页中有效文本的方法 【NLTK基础教程】01-1 三种获取网页中有效文本的方法
本篇是《NLTK基础教程》第一章的第三节,向NLTK迈进中的一个例子,获取网页中的文本的三种方法。 1 抓取网页这里主要是用了urllib库中的函数来抓取指定网页,代码如下: import urllib.request response=u
下一篇 
【自然语言处理入门】03:利用线性回归对数据集进行分析预测(下) 【自然语言处理入门】03:利用线性回归对数据集进行分析预测(下)
上一篇中我们简单的介绍了利用线性回归分析并预测波士顿房价数据集,那么在这一篇中,将使用相同的模型来对红酒数据集进行分析。 1 基本要求利用线性回归,对红酒数据集进行分析。数据集下载地址。 2 完整代码#-*- coding: UTF-8
  目录